package com.ruoyi.base.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.base.domain.FinancialDefaultAccount;
import com.ruoyi.base.mapper.FinancialDefaultAccountMapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.AccountSetsMapper;
import com.ruoyi.base.domain.AccountSets;
import com.ruoyi.base.service.IAccountSetsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 账套Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-24
 */
@Service
@Transactional
public class AccountSetsServiceImpl extends ServiceImpl<AccountSetsMapper , AccountSets> implements IAccountSetsService
{
    @Autowired
    private AccountSetsMapper accountSetsMapper;

    @Autowired
    private FinancialDefaultAccountMapper defaultAccountMapper;

    /**
     * 查询账套
     * 
     * @param id 账套主键
     * @return 账套
     */
    @Override
    public AccountSets selectAccountSetsById(Long id)
    {
        return accountSetsMapper.selectAccountSetsById(id);
    }

    /**
     * 查询账套列表
     * 
     * @param accountSets 账套
     * @return 账套
     */
    @Override
    public List<AccountSets> selectAccountSetsList(AccountSets accountSets)
    {
        return accountSetsMapper.selectAccountSetsList(accountSets);
    }

    /**
     * 新增账套
     * 
     * @param accountSets 账套
     * @return 结果
     */
    @Override
    public int insertAccountSets(AccountSets accountSets)
    {

        return accountSetsMapper.insert(accountSets);
    }

    /**
     * 修改账套
     * 
     * @param accountSets 账套
     * @return 结果
     */
    @Override
    public int updateAccountSets(AccountSets accountSets)
    {
        return accountSetsMapper.updateById(accountSets);
    }

    /**
     * 批量删除账套
     * 
     * @param ids 需要删除的账套主键
     * @return 结果
     */
    @Override
    public int deleteAccountSetsByIds(Long[] ids)
    {
        return accountSetsMapper.deleteAccountSetsByIds(ids);
    }

    /**
     * 删除账套信息
     * 
     * @param id 账套主键
     * @return 结果
     */
    @Override
    public int deleteAccountSetsById(Long id)
    {
        return accountSetsMapper.deleteAccountSetsById(id);
    }

    /** 
    * @Description: 查询用户的默认账套
    * @Param: []
    * @return: java.util.List<com.ruoyi.base.domain.AccountSets>
    * @Author: Yuan
    * @Date: 2025/2/25
    */
    @Override
    public List<AccountSets> accountSetsList() {
        Long userId = SecurityUtils.getUserId();
        return accountSetsMapper.accountSetsList(userId);
    }

    /**
    * @Description: 切换账套
    * @Param: [accountId]
    * @return: int
    * @Author: Yuan
    * @Date: 2025/2/25
    */
    @Override
    public int checkAccountSet(Long accountId) throws Exception {
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<FinancialDefaultAccount> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FinancialDefaultAccount::getUserId,userId);
        List<FinancialDefaultAccount> defaultAccounts = defaultAccountMapper.selectList(wrapper);
        if (defaultAccounts.size()>0){
            FinancialDefaultAccount financialDefaultAccount = defaultAccounts.get(0);
            if (financialDefaultAccount.getAccountSetId().equals(accountId)){
                throw new Exception("已经是默认的账套了");
            }
            defaultAccountMapper.delete(wrapper);
        }

        FinancialDefaultAccount defaultAccount = new FinancialDefaultAccount();
        defaultAccount.setUserId(userId);
        defaultAccount.setAccountSetId(accountId);
        return defaultAccountMapper.insert(defaultAccount);
    }
}
